package com.gljx.web.service.impl;


import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.gljx.util.BigDecimalFormat;
import com.gljx.util.GetFid;
import com.gljx.web.Entity.Student;
import com.gljx.web.Entity.TrainCharge;
import com.gljx.web.Entity.TrainChargeEntry;
import com.gljx.web.Entity.JournalEntry.TrainCollect;
import com.gljx.web.dao.CommonDao;
import com.gljx.web.dao.StudentMapper;
import com.gljx.web.dao.TrainChargeEntryMapper;
import com.gljx.web.dao.TrainChargeMapper;
import com.gljx.web.service.TrainChargeService;

@Transactional
@Service
public class TrainChargeServiceImpl  extends CommonDao implements TrainChargeService{

	@Autowired
	private TrainChargeMapper trainChargeMapper;

	@Autowired
	private TrainChargeEntryMapper trainChargeEntryMapper;

	@Autowired
	private StudentMapper studentMapper;


	/**
	 *取分页数据    dmz 20160921
	 *@param pindex 页码
	 *@param psql SQL语句，需要带 rownum rn 例如：select rownum rn,t.* from zzda t
	 *@param psize 每页记录数
	 * */
	@Override
	public Map<String, Object> selectPage(int pindex, String psql, int psize) {
		  Map<String, Object> paramMap = new HashMap<String, Object>();
	       paramMap.put("Pindex", pindex);
	       paramMap.put("Psql", psql);
	       paramMap.put("Psize", psize);
	       paramMap.put("Pcount", oracle.jdbc.OracleTypes.INTEGER);
	       paramMap.put("v_cur", oracle.jdbc.OracleTypes.CURSOR);
	       this.getSqlSession().select("com.gljx.web.dao.TrainChargeMapper.pagemap", paramMap, null);
	       return  paramMap;
	}

	/**
	  取记录总数      dmz 20160921
	 * @param psql   SQL语句
	 * @return
	 */
	@Override
	public Map<String, Object> selectPageCount(String psql) {
		   Map<String, Object> paramMap = new HashMap<String, Object>();
	       paramMap.put("Psql", psql);
	       paramMap.put("Pcount", oracle.jdbc.OracleTypes.INTEGER);
	       this.getSqlSession().select("com.gljx.web.dao.TrainChargeMapper.pagecount", paramMap, null);
	       return paramMap;
	}

	/**
	 * 添加财务管理
	 */
	@Override
	public void addTrainCharge(TrainCharge trainCharge, String[] fseq, String[] cfstudentno, String[] cfstudentid,
			String[] cfsex, String[] cfidcardno,String cfidcardaddress[], String[] cftjr, String[] cfclazz, String[] cfclazzamount,
			String[] cfyishou, String[] cfysje, String[] cfguifei, String[] cfpxf, String[] cfcash, String[] cfcard,
			String[] cfguazhang, String[] cfyhzz, String[] cfshje, String[] cfyhje, String[] cfyhj, String[] cfqfje,
			String[] cfremark) {
		//添加父类
		trainChargeMapper.insertSelective(trainCharge);
		//添加子类
		for (int i = 0; i < fseq.length; i++) {
			TrainChargeEntry trainChargeEntry = new TrainChargeEntry();
			trainChargeEntry.setCfstudentno(cfstudentno[i]);
			trainChargeEntry.setCfstudentid(cfstudentid[i]);
			if("".equals(cfsex[i]) || cfsex.length == 0){

			}else{
				trainChargeEntry.setCfsex(Long.parseLong(cfsex[i]));
			}
			trainChargeEntry.setCfidcardno(cfidcardno[i]);
			trainChargeEntry.setCfidcardaddress(cfidcardaddress[i]);
			trainChargeEntry.setCftjr(cftjr[i]);
			trainChargeEntry.setCfclazz(cfclazz[i]);
			if("".equals(cfclazzamount[i])){

			}else{
				trainChargeEntry.setCfclazzamount(BigDecimalFormat.formot(cfclazzamount[i]));
			}
			if("".equals(cfyishou[i])){

			}else{
				trainChargeEntry.setCfyishou(BigDecimalFormat.formot(cfyishou[i]));
			}
			if("".equals(cfysje[i])){

			}else{
				trainChargeEntry.setCfysje(BigDecimalFormat.formot(cfysje[i]));
			}
			if("".equals(cfguifei[i])){

			}else{
				trainChargeEntry.setCfguifei(BigDecimalFormat.formot(cfguifei[i]));
			}
			if("".equals(cfpxf[i])){

			}else{
				trainChargeEntry.setCfpxf(BigDecimalFormat.formot(cfpxf[i]));
			}
			if("".equals(cfcash[i])){

			}else{
				trainChargeEntry.setCfcash(BigDecimalFormat.formot(cfcash[i]));
			}
			if("".equals(cfcard[i])){

			}else{
				trainChargeEntry.setCfcard(BigDecimalFormat.formot(cfcard[i]));
			}
			if("".equals(cfguazhang[i])){

			}else{
				trainChargeEntry.setCfguazhang(BigDecimalFormat.formot(cfguazhang[i]));
			}
			if("".equals(cfyhzz[i])){

			}else{
				trainChargeEntry.setCfyhzz(BigDecimalFormat.formot(cfyhzz[i]));
			}
			if("".equals(cfshje[i])){

			}else{
				trainChargeEntry.setCfshje(BigDecimalFormat.formot(cfshje[i]));
			}
			if("".equals(cfyhje[i])){

			}else{
				trainChargeEntry.setCfyhje(BigDecimalFormat.formot(cfyhje[i]));
			}
			if("".equals(cfyhj[i])){

			}else{
				trainChargeEntry.setCfyhj(BigDecimalFormat.formot(cfyhj[i]));
			}
			if("".equals(cfqfje[i])){

			}else{
				trainChargeEntry.setCfqfje(BigDecimalFormat.formot(cfqfje[i]));
			}
			trainChargeEntry.setFparentid(trainCharge.getFid());
			trainChargeEntry.setCfremark(cfremark[i]);
			trainChargeEntry.setFid(GetFid.UUIDIntoFid());
			trainChargeEntry.setFseq((long) (i + 1));
			//添加子类
			Student student = new Student();
			student.setFid(cfstudentid[i]);

			if ("".equals(cfyhje[i])){
                cfyhje[i] = "0";
            }
            if ("".equals(cfyhj[i])){
                cfyhj[i] = "0";
            }
            if ("".equals(cfclazzamount[i])){
                cfclazzamount[i] = "0";
            }
            if ("".equals(cfqfje[i])){
                cfqfje[i] = "0";
            }
			//获取优惠金额
			BigDecimal cfyhjes = new BigDecimal(cfyhje[i]);
			//获取优惠券
			BigDecimal yhj = new BigDecimal(cfyhj[i]);
			//获取班级金额
			BigDecimal clazzamount = new BigDecimal(cfclazzamount[i]);
			//欠费金额
			student.setCfarrearage(new BigDecimal(cfqfje[i]));
            //设置优惠金额
			BigDecimal cfyhje2 = cfyhjes.add(yhj);
			student.setCffavorable(cfyhje2);
			//设置已收金额
			BigDecimal yhje = clazzamount.subtract(cfyhje2).subtract(trainChargeEntry.getCfqfje());
			student.setCfreceived(yhje);
			studentMapper.updateByPrimaryKeySelective(student);

			trainChargeEntryMapper.insertSelective(trainChargeEntry);
		}
	}

	/**
	 * 根据id查询
	 */
	@Override
	public TrainCharge queryByTrainChargeFid(String fid) {
		// TODO Auto-generated method stub
		return trainChargeMapper.selectByPrimaryKey(fid);
	}

	/**
	 * 修改财务管理
	 */
	@Override
	public void modifyTrainCharge(TrainCharge trainCharge, String[] fseq, String[] cfstudentno, String[] cfstudentid,
			String[] cfsex, String[] cfidcardno, String[] cfidcardaddress, String[] cftjr, String[] cfclazz,
			String[] cfclazzamount, String[] cfyishou, String[] cfysje, String[] cfguifei, String[] cfpxf,
			String[] cfcash, String[] cfcard, String[] cfguazhang, String[] cfyhzz, String[] cfshje, String[] cfyhje,
			String[] cfyhj, String[] cfqfje, String[] cfremark) {
		//修改父类
		trainChargeMapper.updateByPrimaryKeySelective(trainCharge);
		//根据父类id删除
		trainChargeEntryMapper.deleteByFparentid(trainCharge.getFid());
		//添加子类
		if(fseq == null || fseq.length == 0){

		}else{
			for (int i = 0; i < fseq.length; i++) {
				TrainChargeEntry trainChargeEntry = new TrainChargeEntry();
				trainChargeEntry.setCfstudentno(cfstudentno[i]);
				trainChargeEntry.setCfstudentid(cfstudentid[i]);
				if("".equals(cfsex[i]) || cfsex.length == 0){

				}else{
					trainChargeEntry.setCfsex(Long.parseLong(cfsex[i]));
				}
				trainChargeEntry.setCfidcardno(cfidcardno[i]);
				trainChargeEntry.setCfidcardaddress(cfidcardaddress[i]);
				trainChargeEntry.setCftjr(cftjr[i]);
				trainChargeEntry.setCfclazz(cfclazz[i]);
				if("".equals(cfclazzamount[i])){

				}else{
					trainChargeEntry.setCfclazzamount(BigDecimalFormat.formot(cfclazzamount[i]));
				}
				if("".equals(cfyishou[i])){

				}else{
					trainChargeEntry.setCfyishou(BigDecimalFormat.formot(cfyishou[i]));
				}
				if("".equals(cfysje[i])){

				}else{
					trainChargeEntry.setCfysje(BigDecimalFormat.formot(cfysje[i]));
				}
				if("".equals(cfguifei[i])){

				}else{
					trainChargeEntry.setCfguifei(BigDecimalFormat.formot(cfguifei[i]));
				}
				if("".equals(cfpxf[i])){

				}else{
					trainChargeEntry.setCfpxf(BigDecimalFormat.formot(cfpxf[i]));
				}
				if("".equals(cfcash[i])){

				}else{
					trainChargeEntry.setCfcash(BigDecimalFormat.formot(cfcash[i]));
				}
				if("".equals(cfcard[i])){

				}else{
					trainChargeEntry.setCfcard(BigDecimalFormat.formot(cfcard[i]));
				}
				if("".equals(cfguazhang[i])){

				}else{
					trainChargeEntry.setCfguazhang(BigDecimalFormat.formot(cfguazhang[i]));
				}
				if("".equals(cfyhzz[i])){

				}else{
					trainChargeEntry.setCfyhzz(BigDecimalFormat.formot(cfyhzz[i]));
				}
				if("".equals(cfshje[i])){

				}else{
					trainChargeEntry.setCfshje(BigDecimalFormat.formot(cfshje[i]));
				}
				if("".equals(cfyhje[i])){

				}else{
					trainChargeEntry.setCfyhje(BigDecimalFormat.formot(cfyhje[i]));
				}
				if("".equals(cfyhj[i])){

				}else{
					trainChargeEntry.setCfyhj(BigDecimalFormat.formot(cfyhj[i]));
				}
				if("".equals(cfqfje[i])){

				}else{
					trainChargeEntry.setCfqfje(BigDecimalFormat.formot(cfqfje[i]));
				}
				trainChargeEntry.setFparentid(trainCharge.getFid());
				trainChargeEntry.setCfremark(cfremark[i]);
				trainChargeEntry.setFid(GetFid.UUIDIntoFid());
				trainChargeEntry.setFseq((long) (i + 1));
				//添加子类
				trainChargeEntryMapper.insertSelective(trainChargeEntry);
			}
		}

	}

	/**
	 * 审核
	 */
	@Override
    public int auditTrainCharge(List<TrainChargeEntry> trainChargeEntries, TrainCharge trainCharge) {
        /**
		 * 修改学生
		 */
		for (TrainChargeEntry trainChargeEntry : trainChargeEntries) {
			Student student = new Student();
			student.setFid(trainChargeEntry.getCfstudentid());
			//获取优惠金额
			BigDecimal cfyhje = trainChargeEntry.getCfyhje();
            //获取优惠券
            BigDecimal yhj = trainChargeEntry.getCfyhj();
            //获取班级金额
            BigDecimal Clazzamount = trainChargeEntry.getCfclazzamount();
            //欠费金额
			student.setCfarrearage(trainChargeEntry.getCfqfje());
			if (cfyhje == null) {
                cfyhje = new BigDecimal(0);
            }
            if (yhj == null) {
                yhj = new BigDecimal(0);
            }
            //设置优惠金额
            BigDecimal cfyhje2 = cfyhje.add(yhj);
            student.setCffavorable(cfyhje2);
            //设置已收金额
            BigDecimal yhje = Clazzamount.subtract(cfyhje2).subtract(trainChargeEntry.getCfqfje());
            student.setCfreceived(yhje);
            studentMapper.updateByPrimaryKeySelective(student);
		}
		//修改父类
        return trainChargeMapper.updateByPrimaryKeySelective(trainCharge);
    }


	@Override
	public List<TrainCollect> queryTrainChargeByTime(String companyId, String beginTime, String endTime) {
		// TODO Auto-generated method stub
		Map<String, String> map = new HashMap<>();
		map.put("beginTime", beginTime);
		map.put("endTime", endTime);
		map.put("companyId", companyId);
		return trainChargeMapper.selectTrainChargeByTime(map);
	}

	@Override
	public List<TrainCharge> selectTrainCharge(Map<String, String> map) {
		// TODO Auto-generated method stub
		return trainChargeMapper.selectTrainCharge(map);
	}

	@Override
	public List<TrainCollect> queryMakeupChargeByTime(String companyId, String beginTime, String endTime) {
		// TODO Auto-generated method stub
		Map<String, String> map = new HashMap<>();
		map.put("beginTime", beginTime);
		map.put("endTime", endTime);
		map.put("companyId", companyId);
		return trainChargeMapper.selectMakeupChargeByTime(map);
	}

	@Override
	public List<TrainCollect> queryWillmigerlByTime(String companyId, String beginTime, String endTime) {
		// TODO Auto-generated method stub
		Map<String, String> map = new HashMap<>();
		map.put("beginTime", beginTime);
		map.put("endTime", endTime);
		map.put("companyId", companyId);
		return trainChargeMapper.selectWillmigerlByTime(map);
	}

	@Override
	public List<TrainCollect> queryTranSferByTime(String companyId, String beginTime, String endTime) {
		// TODO Auto-generated method stub
		Map<String, String> map = new HashMap<>();
		map.put("beginTime", beginTime);
		map.put("endTime", endTime);
		map.put("companyId", companyId);
		return trainChargeMapper.selectTranSferByTime(map);
	}

	@Override
	public List<TrainCollect> queryTurnexamCostByTime(String companyId, String beginTime, String endTime) {
		// TODO Auto-generated method stub
		Map<String, String> map = new HashMap<>();
		map.put("beginTime", beginTime);
		map.put("endTime", endTime);
		map.put("companyId", companyId);
		return trainChargeMapper.selectTurnexamCostByTime(map);
	}

	@Override
	public List<TrainCollect> queryReturnPremiumByTime(String companyId, String beginTime, String endTime) {
		// TODO Auto-generated method stub
		Map<String, String> map = new HashMap<>();
		map.put("beginTime", beginTime);
		map.put("endTime", endTime);
		map.put("companyId", companyId);
		return trainChargeMapper.selectReturnPremiumByTime(map);
	}

	@Override
	public List<TrainCollect> queryOwnCarTrainByTime(String companyId, String beginTime, String endTime,
			Integer kemu) {
		// TODO Auto-generated method stub
		Map<String, String> map = new HashMap<>();
		map.put("beginTime", beginTime);
		map.put("endTime", endTime);
		map.put("companyId", companyId);
		map.put("kemu", kemu.toString());
		return trainChargeMapper.selectOwnCarTrainByTime(map);
	}

	@Override
	public List<TrainCollect> queryOtherPayDemandByTime(String companyId, String beginTime, String endTime) {
		// TODO Auto-generated method stub
		Map<String, String> map = new HashMap<>();
		map.put("beginTime", beginTime);
		map.put("endTime", endTime);
		map.put("companyId", companyId);
		return trainChargeMapper.selectOtherPayDemandByTime(map);
	}

	@Override
	public List<TrainCollect> queryTimeCardByTime(String companyId, String beginTime, String endTime) {
		// TODO Auto-generated method stub
		Map<String, String> map = new HashMap<>();
		map.put("beginTime", beginTime);
		map.put("endTime", endTime);
		map.put("companyId", companyId);
		return trainChargeMapper.selectTimeCardByTime(map);
	}

	/**
	 * 培训收费反审核
	 * @param trainChargeEntries
	 * @param trainCharge
	 * @return
	 */
	@Override
    public int updateTrainCharge(List<TrainChargeEntry> trainChargeEntries,TrainCharge trainCharge) {
        // TODO Auto-generated method stub

		/**
		 * 修改学生
		 */
		for (TrainChargeEntry trainChargeEntry : trainChargeEntries) {
			Student student = new Student();
			student.setFid(trainChargeEntry.getCfstudentid());
			//获取优惠金额
			BigDecimal cfyhje = trainChargeEntry.getCfyhje();
			//获取优惠券
			BigDecimal yhj = trainChargeEntry.getCfyhj();
			//获取班级金额
			BigDecimal Clazzamount = trainChargeEntry.getCfclazzamount();
			//欠费金额
			student.setCfarrearage(Clazzamount);
			if (cfyhje == null) {
				cfyhje = new BigDecimal(0);
			}
			if (yhj == null) {
				yhj = new BigDecimal(0);
			}
			//设置优惠金额
			BigDecimal cfyhje2 = cfyhje.add(yhj);
			student.setCffavorable(cfyhje2);
			//设置已收金额
			BigDecimal yhje = Clazzamount.subtract(cfyhje2).subtract(trainChargeEntry.getCfqfje());
			student.setCfreceived(new BigDecimal(0));

			studentMapper.updateByPrimaryKeySelective(student);
		}

        return trainChargeMapper.updateByPrimaryKeySelective(trainCharge);
    }

	@Override
	public TrainCharge selectTrainChargePrice(Map<String, String> map) {
		// TODO Auto-generated method stub
		return trainChargeMapper.selectTrainChargePrice(map);
	}

	@Override
	public int insertSelective(TrainCharge train) {

		return trainChargeMapper.insertSelective(train);
	}


}
